package com.hq.door.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hq.door.dao.TClassDAO;
import com.hq.door.dao.TInstituteDAO;
import com.hq.door.dao.TStudentDAO;
import com.hq.door.dto.InstituteInfo;
import com.hq.door.dto.StudentInfo;
import com.hq.door.pojo.TClass;
import com.hq.door.pojo.TInstitute;
import com.hq.door.pojo.TParent;
import com.hq.door.pojo.TStudent;
import com.hq.door.service.StudentBackService;

/**
 * 后台学生管理模块服务实现类
 * @author Administrator
 *
 */
@Service("studentBackService")
public class StudentBackServiceImpl extends BaseServiceImpl<TStudent> implements
		StudentBackService {
	private TStudentDAO tStudentDAO;

	@Autowired
	private TClassDAO tClassDAO;

	public TClassDAO gettClassDAO() {
		return tClassDAO;
	}

	public void settClassDAO(TClassDAO tClassDAO) {
		this.tClassDAO = tClassDAO;
	}

	public TStudentDAO gettStudentDAO() {
		return tStudentDAO;
	}

	@Autowired
	public void settStudentDAO(TStudentDAO tStudentDAO) {
		this.tStudentDAO = tStudentDAO;
		this.setBaseDao(tStudentDAO);
	}


	@Override
	public StudentInfo getStudentInfo(int id) {

		TStudent tStudent = tStudentDAO.get(id);

		StudentInfo studentInfo = new StudentInfo();
		// studentInfo.getStudentNameAndClass(tStudent);

		return studentInfo;
	}

	@Override
	public List<StudentInfo> getByPage(int type, String []para, int pageStart,
			int pageSize) {

		String hql;
		List<StudentInfo> studentInfos = null;
		List<TStudent> tStudents = null;
		List<String> conditions = new ArrayList<String>();
		pageStart = (pageStart - 1) * pageSize;
		
		hql="select distinct st from TStudent st inner join fetch st.TClass c inner join fetch c.TMajor m inner join fetch m.TInstitute ins, TParent p";
		
		if("".equals(para[0])&&"".equals(para[1])&&"".equals(para[2])&&"".equals(para[3])&&"".equals(para[4])){
			
		}else{
			hql = hql +" where";
		}
		
		int single = 0;
		
		for(int i = 0; i < para.length; i++){
			if(!"".equals(para[i])){
				single++;
				if(single>1){
					hql = hql + " and";
				}
				switch (i) {
				case 0:
					hql = hql + " ins.name=?";
					conditions.add(para[i]);
					break;
				case 1:
					hql = hql + " m.name=?";
					conditions.add(para[i]);
					break;
				case 2:
					hql = hql + " c.name=?";
					conditions.add(para[i]);
					break;
				case 3:
					hql = hql + " c.grade=?";
					conditions.add(para[i]);
					break;
				case 4:
					hql = hql + " (st.name like ? or st.code like ? or p in elements (st.TParents) and p.name like ?)";
					conditions.add("%"+para[i]+"%");
					conditions.add("%"+para[i]+"%");
					conditions.add("%"+para[i]+"%");
					break;
				default:
					break;
				}
			}
		}
		
		tStudents = tStudentDAO.findPageObjects(hql, pageStart, pageSize,conditions.toArray());
		studentInfos = new StudentInfo().getStudentByPage(tStudents);
		
//		switch (type) {
//		case 0:
////			hql = "select st from TStudent st";
//			tStudents = tStudentDAO.findPageObjects(hql, pageStart, pageSize);
//			studentInfos = new StudentInfo().getStudentByPage(tStudents);
//			break;
//		case 1:
//			hql = "select st from TStudent st where st.code like ?";
//			para = "%" + para + "%";
//			tStudents = tStudentDAO.findPageObjects(hql, pageStart, pageSize,
//					para);
//			studentInfos = new StudentInfo().getStudentByPage(tStudents);
//			break;
//		case 2:
//			// hql =
//			// "select st from TStudent st inner join fetch st.TParents ps where st.name like ? or ps.";
//			hql = "select distinct st from TStudent st, TParent p where st.name like ? or p in elements (st.TParents) and p.name like ?";
//			para = "%" + para + "%";
//			tStudents = tStudentDAO.findPageObjects(hql, pageStart, pageSize,
//					para, para);
//			studentInfos = new StudentInfo().getStudentByPage(tStudents);
//			break;
//		case 3:
//			hql = "select st from TStudent st inner join fetch st.TClass c inner join fetch c.TMajor m inner join fetch m.TInstitute ins where ins.name=?";
//			tStudents = tStudentDAO.findPageObjects(hql, pageStart, pageSize,
//					para);
//			studentInfos = new StudentInfo().getStudentByPage(tStudents);
//			break;
//		case 4:
//			hql = "select st from TStudent st inner join fetch st.TClass c inner join fetch c.TMajor m where m.name=?";
//			tStudents = tStudentDAO.findPageObjects(hql, pageStart, pageSize,
//					para);
//			studentInfos = new StudentInfo().getStudentByPage(tStudents);
//			break;
//		case 5:
//			hql = "select st from TStudent st inner join fetch st.TClass c where c.grade=?";
//			tStudents = tStudentDAO.findPageObjects(hql, pageStart, pageSize,
//					para);
//			studentInfos = new StudentInfo().getStudentByPage(tStudents);
//			break;
//		case 6:
//			hql = "select st from TStudent st inner join fetch st.TClass c where c.name=?";
//			tStudents = tStudentDAO.findPageObjects(hql, pageStart, pageSize,
//					para);
//			studentInfos = new StudentInfo().getStudentByPage(tStudents);
//			break;
//		default:
//			return studentInfos;
//		}
		return studentInfos;
	}

	@Override
	public int getCount(int type, String []para) {

		int count=0;
		String hql;
		List<String> conditions = new ArrayList<String>();
		
		hql="select count(distinct st.id) from TStudent st inner join st.TClass c inner join c.TMajor m inner join m.TInstitute ins, TParent p";
		
		if("".equals(para[0])&&"".equals(para[1])&&"".equals(para[2])&&"".equals(para[3])&&"".equals(para[4])){
			
		}else{
			hql = hql +" where";
		}
		
		int single = 0;
		
		for(int i = 0; i < para.length; i++){
			if(!"".equals(para[i])){
				single++;
				if(single>1){
					hql = hql + " and";
				}
				switch (i) {
				case 0:
					hql = hql + " ins.name=?";
					conditions.add(para[i]);
					break;
				case 1:
					hql = hql + " m.name=?";
					conditions.add(para[i]);
					break;
				case 2:
					hql = hql + " c.name=?";
					conditions.add(para[i]);
					break;
				case 3:
					hql = hql + " c.grade=?";
					conditions.add(para[i]);
					break;
				case 4:
					hql = hql + " (st.name like ? or st.code like ? or p in elements (st.TParents) and p.name like ?)";
					conditions.add("%"+para[i]+"%");
					conditions.add("%"+para[i]+"%");
					conditions.add("%"+para[i]+"%");
					break;
				default:
					break;
				}
			}
		}
		
		count = tStudentDAO.getCount(hql, conditions.toArray());
//		switch (type) {
//		case 0:
//			hql = "select count(st.id) from TStudent st";
//			count = tStudentDAO.getCount(hql);
//			break;
//		case 1:
//			hql = "select count(st.id) from TStudent st where st.code like ?";
//			para = "%" + para + "%";
//			count = tStudentDAO.getCount(hql, para);
//			break;
//		case 2:
//			hql = "select count(distinct st.id)from TStudent st, TParent p where st.name like ? or p in elements (st.TParents) and p.name like ?";
//			para = "%" + para + "%";
//			count = tStudentDAO.getCount(hql, para, para);
//			break;
//		case 3:
//			hql = "select count(st.id) from TStudent st inner join st.TClass c inner join c.TMajor m inner join m.TInstitute ins where ins.name=?";
//			count = tStudentDAO.getCount(hql, para);
//			break;
//		case 4:
//			hql = "select count(st.id) from TStudent st inner join st.TClass c inner join c.TMajor m where m.name=?";
//			count = tStudentDAO.getCount(hql, para);
//			break;
//		case 5:
//			hql = "select count(st.id) from TStudent st inner join st.TClass c where c.grade=?";
//			count = tStudentDAO.getCount(hql, para);
//			break;
//		case 6:
//			hql = "select count(st.id) from TStudent st inner join st.TClass c where c.name=?";
//			count = tStudentDAO.getCount(hql, para);
//			break;
//		case 7:
//			hql = "select count(st.id) from TStudent st where st.code=";
//			count = tStudentDAO.getCount(hql, para);
//			break;
//		default:
//			return 0;
//		}

		return count;
	}

	@Override
	public int updateInfo(StudentInfo studentInfo) {
		String hql = "from TStudent where code=?";
		TStudent tStudent = tStudentDAO.queryOne(hql, studentInfo.getCode());
		hql = "from TClass where name=?";
		TClass tClass = tClassDAO.queryOne(hql, studentInfo.getClassname());
		if (tClass == null) {
			return -1;
		}
		if (tStudent != null) {
			tStudent.setTClass(tClass);
			studentInfo.transformToTStudent(tStudent);
			tStudentDAO.update(tStudent);
			return 0;
		} else {
			tStudent = new TStudent(studentInfo.getName(),
					studentInfo.getCode(), studentInfo.getMobile());
			tStudent.setTClass(tClass);
			tStudentDAO.save(tStudent);
			return 1;
		}

	}
	
	@Override
	public int updateStuPossWord(String code,String possWord) {
		String hql = "from TStudent where code=?";
		TStudent tStudent = tStudentDAO.queryOne(hql, code);		
		if (tStudent != null) {
			tStudent.setPassword(possWord);
			tStudentDAO.update(tStudent);
			return 0;
		} else {
			return 1;
		}
	}

	@Override
	public int delStudent(String code) {
		String hql = "from TStudent where code=?";
		TStudent tStudent = tStudentDAO.queryOne(hql, code);
		if (tStudent != null) {
			tStudentDAO.delete(tStudent);
			return 1;
		}
		return 0;
	}

	@Override
	public int delBatchStudent(String[] codes) {
		int n = 0;
		for (String code : codes) {
			String hql = "from TStudent where code=?";
			TStudent tStudent = tStudentDAO.queryOne(hql, code);
			if (tStudent != null) {
				tStudentDAO.delete(tStudent);
				n++;
			}
		}
		return n;
	}

	@Override
	public StudentInfo getOneStu(String code) {
		String hql = "from TStudent where code=?";
		TStudent tStudent = tStudentDAO.queryOne(hql, code);
		StudentInfo studentInfo = null;
		if(tStudent!=null){
			studentInfo = new StudentInfo();
			studentInfo.getStudentOne(tStudent);
		}
		return studentInfo;
	}

}
